import math
import common

def AABB_AABB(a,b):

    xdist = b.x - a.x
    xvector = a.xw+b.xw - abs(xdist)
    ydist = b.y - a.y
    yvector = a.yw+b.yw - abs(ydist)
    
    if xdist < 0 and xvector > 0 and yvector > 0 and xvector < yvector:
        b.x -= 2
    if xdist > 0 and xvector > 0 and yvector > 0 and xvector < yvector:
        b.x += 2
        
    if ydist < 0 and yvector > 0 and xvector > 0 and xvector > yvector:
        b.y -= 2
    if ydist > 0 and yvector > 0 and xvector > 0 and xvector > yvector:
        b.y += 2 

        
def TRI_AABB(a,b):
    vectors = []
    aL = a.angles[0] - math.pi/2
    aD = common.get_dist(a.points[1][0]- a.points[2][0], a.points[1][1] - a.points[2][1]) * math.sin(a.angles[1])
    bL = a.angles[1] - math.pi/2
    bD = common.get_dist(a.points[2][0]- a.points[0][0], a.points[2][1] - a.points[0][1]) * math.sin(a.angles[1])
    cL = a.angles[2] - math.pi/2
    cD = common.get_dist(a.points[0][0]- a.points[1][0], a.points[0][1] - a.points[1][1]) * math.sin(a.angles[2])
    #print aD,bD,cD
    
    aDD = common.get_dist(b.points[1][0]- b.points[2][0], b.points[1][1] - b.points[2][1]) * math.sin(math.pi/2)
    bDD = -common.get_dist(b.points[1][0]- b.points[2][0], b.points[1][1] - b.points[2][1]) * math.sin(a.angles[1])*2
    cDD = common.get_dist(b.points[0][0]- b.points[1][0], b.points[0][1] - b.points[1][1]) * math.sin(math.pi/2)
    
    
    yVector1 =  b.points[2][1]-(a.points[2][1]+common.get_xy(cL,cD)[1])
    yVector2 = (b.points[2][1]+common.get_xy(cL,cDD)[1]) - a.points[2][1]
    
    xVector1 = b.points[0][0] - (a.points[2][0]+common.get_xy(aL,aD)[0])
    xVector2 = (b.points[0][0]+common.get_xy(bL,bDD)[0]) - a.points[2][0]
    
    ax1 = a.points[0][0]
    ax2 = a.points[0][0]+common.get_xy(bL,bD)[0]
    ay1 = a.points[0][1]
    ay2 = a.points[0][1]+common.get_xy(bL,bD)[1]
    bx1 = b.points[2][0]-ax2
    bx2 = b.points[2][0]-common.get_xy(bL,bDD)[0]
    by1 = b.points[2][1]-ay2
    by2 = b.points[2][1]-common.get_xy(bL,bDD)[1]
    
    dp = (bx1*ax1)+(by1*ay1)
    print dp, xVector1, xVector2, yVector1, yVector2   
    
    if 42700 >= dp > 0 and xVector1 < 0 and xVector2 > 5 and yVector1 > 5 and yVector2 < 0:
        a.x +=2
        a.y +=2
    elif dp > 0 and xVector1 < 0 and -1 < xVector2 < 5 and yVector1 > 0 and yVector2 < 0:
        a.x += 2
    elif dp > 0 and xVector1 < 0 and xVector2 > 0 and -1 < yVector1 < 5 and yVector2 < 0:
        a.y += 2
    if 42700 < dp and xVector1 < 0 and xVector2 > 0 and yVector1 > 0 and yVector2 < 0 and yVector2 < xVector1:
        a.x -= 2
    if 42700 < dp and xVector1 < 0 and xVector2 > 0 and yVector1 > 0 and yVector2 < 0 and yVector2 > xVector1:
        a.y -= 2
        
def TRI_AABB2(a,b):
    vectors = []
    aL = a.angles[0] - math.pi/2
    aD = common.get_dist(a.points[1][0]- a.points[2][0], a.points[1][1] - a.points[2][1]) * math.sin(a.angles[1])
    bL = a.angles[1] - math.pi/2
    bD = common.get_dist(a.points[2][0]- a.points[0][0], a.points[2][1] - a.points[0][1]) * math.sin(a.angles[1])
    cL = a.angles[2] - math.pi/2
    cD = common.get_dist(a.points[0][0]- a.points[1][0], a.points[0][1] - a.points[1][1]) * math.sin(a.angles[2])
    #print aD,bD,cD
    
    aDD = common.get_dist(b.points[1][0]- b.points[2][0], b.points[1][1] - b.points[2][1]) * math.sin(math.pi/2)
    bDD = -common.get_dist(b.points[1][0]- b.points[2][0], b.points[1][1] - b.points[2][1]) * math.sin(a.angles[1])*2
    cDD = common.get_dist(b.points[0][0]- b.points[1][0], b.points[0][1] - b.points[1][1]) * math.sin(math.pi/2)
    
    
    yVector1 =  b.points[2][1]-(a.points[2][1]+common.get_xy(cL,cD)[1])
    yVector2 = (b.points[2][1]+common.get_xy(cL,cDD)[1]) - a.points[2][1]
    
    xVector1 = b.points[0][0] - (a.points[2][0]+common.get_xy(aL,aD)[0])
    xVector2 = (b.points[0][0]+common.get_xy(bL,bDD)[0]) - a.points[2][0]
    
    ax1 = a.points[0][0]
    ax2 = a.points[0][0]+common.get_xy(bL,bD)[0]
    ay1 = a.points[0][1]
    ay2 = a.points[0][1]+common.get_xy(bL,bD)[1]
    bx1 = b.points[2][0]-ax2
    bx2 = b.points[2][0]-common.get_xy(bL,bDD)[0]
    by1 = b.points[2][1]-ay2
    by2 = b.points[2][1]-common.get_xy(bL,bDD)[1]
    
    dp = (bx1*ax1)+(by1*ay1)
    print dp, xVector1, xVector2, yVector1, yVector2   
    
    if 42700 >= dp > 0 and xVector1 < 0 and xVector2 > 5 and yVector1 > 5 and yVector2 < 0:
        b.x -=2
        b.y -=2
    elif dp > 0 and xVector1 < 0 and -1 < xVector2 < 5 and yVector1 > 0 and yVector2 < 0:
        b.x -= 2
    elif dp > 0 and xVector1 < 0 and xVector2 > 0 and -1 < yVector1 < 5 and yVector2 < 0:
        b.y -= 2
    if 42700 < dp and xVector1 < 0 and xVector2 > 0 and yVector1 > 0 and yVector2 < 0 and yVector2 < xVector1:
        b.x += 2
    if 42700 < dp and xVector1 < 0 and xVector2 > 0 and yVector1 > 0 and yVector2 < 0 and yVector2 > xVector1:
        b.y += 2
                